
package com.jf.cloud.service.sysmanager.remote.api;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jf.cloud.service.sysmanager.common.db.Query;
import com.jf.cloud.service.sysmanager.common.util.DateUtil;
import com.jf.cloud.service.sysmanager.common.util.JsonUtil;
import com.jf.cloud.service.sysmanager.common.util.RegexValidUtil;
import com.jf.cloud.service.sysmanager.common.util.StringUtil;
import com.jf.cloud.service.sysmanager.constant.PubgConstant;
import com.jf.cloud.service.sysmanager.constant.UserConstant;
import com.jf.cloud.service.sysmanager.dao.IPubgOperateLogService;
import com.jf.cloud.service.sysmanager.dao.IPubgUserClubService;
import com.jf.cloud.service.sysmanager.dao.IPubgUserRealNameService;
import com.jf.cloud.service.sysmanager.dao.IPubgUserService;
import com.jf.cloud.service.sysmanager.dao.IPubgUserTeamService;
import com.jf.cloud.service.sysmanager.entity.PubgUser;
import com.jf.cloud.service.sysmanager.entity.PubgUserClub;
import com.jf.cloud.service.sysmanager.entity.PubgUserRealName;
import com.jf.cloud.service.sysmanager.entity.PubgUserTeam;
import com.jf.cloud.service.sysmanager.handler.TokenHandler;
import com.jf.cloud.service.sysmanager.support.entity.Result;
import com.jf.cloud.service.sysmanager.support.web.BaseController;

/**
 * 队员-实名认证 控制器
 * 
 * @author 朱建文 创建时间：2018年4月25日 下午7:11:18
 */
@Controller
@RequestMapping("/appUserReal")
public class AppPubgUserRealNameController extends BaseController {

	/**
	 * 日志
	 */
	private static Log log = LogFactory.getLog(AppPubgUserRealNameController.class);

	/**
	 * 队员-实名认证服务
	 */
	@Resource
	private IPubgUserRealNameService pubgUserRealNameService;

	@Resource
	private TokenHandler tokenHandler;

	@Resource
	private IPubgUserService pubgUserService;
	
	@Resource
	private IPubgUserTeamService pubgUserTeamService;
	
	@Resource
	private IPubgUserClubService pubgUserClubService;

	@Resource
	private IPubgOperateLogService pubgOperateLogService;
	
	/**
	 * 提交实名信息
	 * 
	 * @author 朱建文 创建时间：2018年4月26日 上午10:31:56
	 */
	@ResponseBody
	@RequestMapping("/applyRealName")
	public Result<String> applyRealName(String token, PubgUserRealName pubgUserRealName, HttpServletRequest request){
		Result<String> result = new Result<String>();
		result.setCode(-1);
		result.setMessage("提交实名信息失败！");
		try {
	
			// 验证token
			String userId = tokenHandler.getCommUserInfoByToken(token);
			if (userId == null || userId.trim().length() == 0) {
				result.setCode(-2);
				result.setMessage("请重新登陆！");
				return result;
			}
			
			// 验证队员角色
			PubgUser pubgUser = this.pubgUserService.findByPK(Long.valueOf(userId));
			if (pubgUser == null) {
				result.setMessage("帐号信息有误！");
				return result;
			}
			if (!UserConstant.USER_ROLE_10.getId().equals(pubgUser.getUserRole())) {
				result.setMessage("您不需要实名验证！");
				return result;
			}
			
			// 验证实名信息
			result = checkRealNameParam(result, pubgUserRealName);
			if (result.getCode() != 1) {
				return result;
			}
			result.setCode(-1);
			
			// 验证实名信息中选的战队 是否是 队员所在战队
			Query query = new Query();
			query.put("USER_ID", userId);
			query.put("TEAM_ID", pubgUserRealName.getTeamId());
			query.put("RELATION_STATE", PubgConstant.USER_TEAM_RELATION_STATE_2);
			PubgUserTeam pubgUserTeam = this.pubgUserTeamService.find(query);
			if (pubgUserTeam == null) {
				result.setMessage("您所选战队有误！");
				return result;
			}
			
			query = new Query();
			query.put("CLUB_ID", pubgUserTeam.getClubId());
			query.put("USER_ID", userId);
			query.put("AUDIT_STATE", UserConstant.USER_REAL_STATE_20.getId());
			List<PubgUserRealName> isExistList = this.pubgUserRealNameService.findList(query);
			if (isExistList != null && !isExistList.isEmpty()) {
				result.setMessage("请勿重复提交！");
				return result;
			}
			
			// 补充实名信息
			pubgUserRealName.setClubId(pubgUserTeam.getClubId());
			pubgUserRealName.setClubName(pubgUserTeam.getClubName());
			pubgUserRealName.setTeamName(pubgUserTeam.getTeamName());
			pubgUserRealName.setUserId(pubgUserTeam.getUserId());
			pubgUserRealName.setUserAccount(pubgUserTeam.getUserAccount());
			pubgUserRealName.setInviteFrom(pubgUser.getInfoCompleteFlag());
			pubgUserRealName.setApplyTime(DateUtil.getCurrentDateTime());
			pubgUserRealName.setAuditState(UserConstant.USER_REAL_STATE_10.getId());
			
			// 保存实名信息
			this.pubgUserRealNameService.saveOrUpdateUserRealNameInfo(pubgUserRealName);
			result.setCode(0);
			result.setMessage("提交实名信息成功！");
			
			// 插入操作记录
			pubgOperateLogService.insertUserOperateLog(pubgUser.getRowId(), null, "提交实名信息：" + JsonUtil.toJSON(pubgUserRealName));
		} catch (Exception e) {
			log.error("提交实名信息出错！", e);
		}
		return result;
	}

	/**
	 * 验证实名认证信息是否完整
	 * 
	 * @author 朱建文 创建时间：2018年4月26日 上午10:59:25
	 */
	private Result<String> checkRealNameParam(Result<String> result, PubgUserRealName pubgUserRealName){
		
		if (StringUtil.isBlank(pubgUserRealName.getTeamId() + "")) {
			result.setMessage("请选择战队！");
			return result;
		}
		
		if (StringUtil.isBlank(pubgUserRealName.getUserName())) {
			result.setMessage("请填写名称！");
			return result;
		}
		
		if (StringUtil.isBlank(pubgUserRealName.getUserIdCard())) {
			result.setMessage("请填写身份证号码！");
			return result;
		}
		if (!RegexValidUtil.compile(RegexValidUtil.idcard18, pubgUserRealName.getUserIdCard())) {
			result.setMessage("身份证号码不合法！");
			return result;
		}
		
		if (StringUtil.isBlank(pubgUserRealName.getUserIdCardImg())) {
			result.setMessage("请选择身份证照片！");
			return result;
		}
		if (!RegexValidUtil.compile(RegexValidUtil.url, pubgUserRealName.getUserIdCardImg())
				|| !RegexValidUtil.compile(RegexValidUtil.picture, pubgUserRealName.getUserIdCardImg())) {
			result.setMessage("身份证照片不合法！");
			return result;
		}
		result.setCode(1);
		return result;
	}
	
	/**
	 * 获取个人实名认证信息
	 * 
	 * @author 朱建文 创建时间：2018年4月26日 下午3:54:11
	 */
	@ResponseBody
	@RequestMapping("/getUserRealName")
	public Result<PubgUserRealName> getPubgUserRealName(String token, HttpServletRequest request){
		Result<PubgUserRealName> result = new Result<PubgUserRealName>();
		result.setCode(-1);
		result.setMessage("获取个人实名认证信息失败！");

		try {
			// 验证token
			String userId = tokenHandler.getCommUserInfoByToken(token);
			if (userId == null || userId.trim().length() == 0) {
				result.setCode(-2);
				result.setMessage("请重新登陆！");
				return result;
			}
			
			// 验证队员角色
			PubgUser pubgUser = this.pubgUserService.findByPK(Long.valueOf(userId));
			if (pubgUser == null) {
				result.setMessage("帐号信息有误！");
				return result;
			}
			if (!UserConstant.USER_ROLE_10.getId().equals(pubgUser.getUserRole())) {
				result.setMessage("您不需要实名验证！");
				return result;
			}
			
			Query query = new Query();
			query.put("USER_ID", userId);
			PubgUserClub pubgUserClub = this.pubgUserClubService.find(query);
			if (pubgUserClub == null) {
				result.setMessage("请先加入战队！");
				return result;
			}
			
			int auditState = 10;
			if (1 == pubgUser.getRealNameFlag()) {
				auditState = 20;
			}
			
			PubgUserRealName pubgUserRealName = this.pubgUserRealNameService.findPubgUserRealName(
					pubgUserClub.getClubId(), Long.valueOf(userId), auditState);
			result.setCode(0);
			result.setData(pubgUserRealName);
			result.setMessage("获取个人实名认证信息成功！");
		} catch (Exception e) {
			log.error("获取个人实名认证信息出错！", e);
		}
		return result;
	}
}
